package cn.xtcode.types.design.framework.tree;

import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * @Author xt-code
 * @Description: AbstractStrategyRouter<T, D, R>
 * @Create 2025/5/6 16:38
 */
public abstract class AbstractMultiThreadStrategyRouter<T, D, R> extends AbstractStrategyRouter<T, D, R> {
    /**
     * @param requestParameter 入参
     * @param dynamicContext   上下文
     * @return result
     * @throws Exception 异常
     *                   只要用户调用受理方法，立刻调用异步数据加载。如果有的话，就加载数据，在调用业务。
     *                   用户只需要实现doApply即可，不用实现apply了
     *                   handler还需要调用这个执行方法才行
     */
    @Override
    public R apply(T requestParameter, D dynamicContext) throws Exception {
        // 异步加载数据
        multiThread(requestParameter, dynamicContext);
        // 业务流程受理
        return doApply(requestParameter, dynamicContext);

    }

    /**
     * 异步加载数据，供子类实现
     */
    protected abstract void multiThread(T requestParameter, D dynamicContext) throws ExecutionException, InterruptedException, TimeoutException;

    /**
     * 业务流程受理，提供子类重写
     */
    protected abstract R doApply(T requestParameter, D dynamicContext) throws Exception;

}
